<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateFreightAreaAPIRequest;
use App\Http\Requests\API\UpdateFreightAreaAPIRequest;
use App\Models\FreightArea;
use App\Repositories\FreightAreaRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class FreightAreaController
 * @package App\Http\Controllers\API
 */

class FreightAreaAPIController extends AppBaseController
{
    /** @var  FreightAreaRepository */
    private $freightAreaRepository;

    public function __construct(FreightAreaRepository $freightAreaRepo)
    {
        $this->freightAreaRepository = $freightAreaRepo;
    }

    /**
     * Display a listing of the FreightArea.
     * GET|HEAD /freightAreas
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->freightAreaRepository->pushCriteria(new RequestCriteria($request));
        $this->freightAreaRepository->pushCriteria(new LimitOffsetCriteria($request));
        $freightAreas = $this->freightAreaRepository->all();

        return $this->sendResponse($freightAreas->toArray(), 'Freight Areas retrieved successfully');
    }

    /**
     * Store a newly created FreightArea in storage.
     * POST /freightAreas
     *
     * @param CreateFreightAreaAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateFreightAreaAPIRequest $request)
    {
        $input = $request->all();

        $freightAreas = $this->freightAreaRepository->create($input);

        return $this->sendResponse($freightAreas->toArray(), 'Freight Area saved successfully');
    }

    /**
     * Display the specified FreightArea.
     * GET|HEAD /freightAreas/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var FreightArea $freightArea */
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            return $this->sendError('Freight Area not found');
        }

        return $this->sendResponse($freightArea->toArray(), 'Freight Area retrieved successfully');
    }

    /**
     * Update the specified FreightArea in storage.
     * PUT/PATCH /freightAreas/{id}
     *
     * @param  int $id
     * @param UpdateFreightAreaAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateFreightAreaAPIRequest $request)
    {
        $input = $request->all();

        /** @var FreightArea $freightArea */
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            return $this->sendError('Freight Area not found');
        }

        $freightArea = $this->freightAreaRepository->update($input, $id);

        return $this->sendResponse($freightArea->toArray(), 'FreightArea updated successfully');
    }

    /**
     * Remove the specified FreightArea from storage.
     * DELETE /freightAreas/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var FreightArea $freightArea */
        $freightArea = $this->freightAreaRepository->findWithoutFail($id);

        if (empty($freightArea)) {
            return $this->sendError('Freight Area not found');
        }

        $freightArea->delete();

        return $this->sendResponse($id, 'Freight Area deleted successfully');
    }
}
